#!/bin/sh

#-------------------------------------#
#  SGT (Software Generation Toolkit)  #
#-------------------------------------#
#                                     #
#      Compile all scenarii files     #
#        for the current target       #
#                                     #
#  GDE 01/04/2004 v3.0                #
#       improve clean function        #
#                                     #
#  GDE 03/07/2003 v2.1                #
#       check \MAK subdirectory       #
#                                     #
#  GDE 22/04/2003 v2.0                #
#       Add clean rule                #
#                                     #
#  GDE 25/09/2002 v1.0                #
#       creation                      #
#                                     #
#-------------------------------------#
# $1: specific option                 #
#-------------------------------------#



#-------------------------------------#
# Script informations
#-------------------------------------#
export SCRIPT_SGT_VER=3.0
export SCRIPT_SGT_DATE=01/04/2004
export SCRIPT_SGT_USER=GDE
export SCRIPT_SGT_NAME=compile_sce
export SCRIPT_SGT_USAGE="Compile all scenarii files for the current target"
#-------------------------------------#




#---------------------
# Create and go to output directory
#---------------------
if [ -d mak ]
then
	mkdir -p -m775 out
	cd out
else
	export MSG_ERROR_SCRIPT=0
	export CMD_SCRIPT=Error
fi
#---------------------




#-------------------------------------#
# Parameters section
#-------------------------------------#


# Default value :
export OPTION_MAK=

#---------------------
# Parameter 2 -> other options
#---------------------
if [ ! -z $2 ] && [ -z $CMD_SCRIPT ]
then
	if [ $2 = -d ]
	then
		export OPTION_MAK=DEPENDENCIES=ON
	else
    	if [ $2 = -clean ]
    	then
    		export OPTION_MAK=CLEAN_SCE
    	else
    		export MSG_ERROR_SCRIPT=2
    		export CMD_SCRIPT=Error
    	fi
    fi
fi

#---------------------
# Parameter 1 -> target makefile
#---------------------
if [ ! -z $1 ] && [ -z $CMD_SCRIPT ]
then
	if [ $1 = -v ]
	then
		export CMD_SCRIPT=Version
	else
		if [ $1 = -h ]
		then
			export CMD_SCRIPT=Help
		else
			if [ $1 = --help ]
			then
				export CMD_SCRIPT=Help
			else
				if [ $1 = -hm ]
				then
					export CMD_SCRIPT=More
				else
					if [ $1 = -u ]
					then
						export CMD_SCRIPT=Usage
					else
						if [ -f ../mak/$1.mak ]
						then
							export TARGET_LIB=$1
						else
							export MSG_ERROR_SCRIPT=1
							export CMD_SCRIPT=Error
						fi
					fi
				fi
			fi
		fi
	fi
fi


#-------------------------------------#
# if no option is defined, display help
#-------------------------------------#
if [ $# -eq 0 ] && [ -z $CMD_SCRIPT ]
then
	export CMD_SCRIPT=Help
fi


#-------------------------------------#
# Process section
#-------------------------------------#
#---------------------
# Build some SCE files
#---------------------
if [ ! -z $TARGET_LIB ] && [ -z $CMD_SCRIPT ]
then
	echo " "
	echo "    $SCRIPT_SGT_USAGE"
	echo " "
	if [ $OPTION_MAK = CLEAN_SCE ]
	then
		make -f ../mak/gen.mak TARGET=$TARGET_LIB clean_sce_res
	else
		make -f ../mak/gen.mak TARGET=$TARGET_LIB $OPTION_MAK compile_sce
	fi
	exit
fi



#-------------------------------------#
# Help script section
#-------------------------------------#
#---------------------
# Help script section
#---------------------

if [ -z $CMD_SCRIPT ]
then
	export CMD_SCRIPT=Version
fi


#---------------------
# Usage
#---------------------
if [ $CMD_SCRIPT = Usage ]
then
	echo "  $SCRIPT_SGT_NAME	$SCRIPT_SGT_USAGE"
	exit
fi

#---------------------
# Version
#---------------------
echo " "
echo "-------------------------------------"
echo " '$SCRIPT_SGT_NAME' (release "$SCRIPT_SGT_VER") for SGT "$SGT_VER". Last update by "$SCRIPT_SGT_USER" on "$SCRIPT_SGT_DATE
echo "  $SCRIPT_SGT_USAGE"
if [ $CMD_SCRIPT = Version ]
then
	exit
fi

#---------------------
# Simple help
#---------------------
if [ $CMD_SCRIPT = Help ]
then
	echo "-------------------------------------"
	echo " $SCRIPT_SGT_NAME [TARGET] [OPTION]"
	echo " "
	echo "  -d      Enable the scenario dependencies for the current library"
	echo "  -clean  Clean all scenario files and others source files"
	echo " "
	echo "-------------------------------------"
	echo "  -h | --help  Display this help "
	echo "  -hm          Display more help about SGT" $SGT_VER
	echo "  -v           Display the version of the current script"
fi

#---------------------
# More help
#---------------------
if [ $CMD_SCRIPT = More ]
then
	make -f ../mak/gen.mak help USE_TARGET_FILE=OFF
fi



#-------------------------------------#
# Error messages section
#-------------------------------------#
if [ $CMD_SCRIPT = Error ]
then
	if [ $MSG_ERROR_SCRIPT = 0 ]
	then
		echo "-------------------------------------"
		echo " "
		echo "ERROR : The root path is not a valid SGT directory. Please check for the \MAK sub-directory"
	fi
	if [ $MSG_ERROR_SCRIPT = 1 ]
	then
		echo "-------------------------------------"
		echo " "
		echo "ERROR : $1 or ../mak/$1.mak doesn't exist !"
	fi
	if [ $MSG_ERROR_SCRIPT = 2 ]
	then
		echo "-------------------------------------"
		echo " "
		echo "ERROR : unknown option. Check syntax use !"
	fi
	echo " "
	echo " Check your command line or see help section -> $SCRIPT_SGT_NAME -h"
fi
